#include "HiFileReceiverServer.h"
#include "HiFileReceiver.h"

HiFileReceiverServer::HiFileReceiverServer(QObject* parent)
	:QTcpServer(parent)
{

}

HiFileReceiverServer::~HiFileReceiverServer()
{

}

void HiFileReceiverServer::setSavePaths(const QStringList& savePaths)
{
	mSavePaths = savePaths;
}

void HiFileReceiverServer::setMinReaminSize(qint64 minReaminSize)
{
	mMinReaminSize = minReaminSize;
}

void HiFileReceiverServer::incomingConnection(qintptr socketDescriptor)
{
	HiFileReceiver* fileReceiver = getFileReceiverManager()->createReceiver(socketDescriptor, mSavePaths, mMinReaminSize);
    if (fileReceiver) {
        QString ip = fileReceiver->getIp();
        connect(fileReceiver, &HiFileReceiver::signalFileReceiveFinished, this, [=](const QString& filePath, const QByteArray& userData) {
            emit signalFileReceiveFinished(ip, filePath, userData);
        });
        connect(fileReceiver, &HiFileReceiver::signalReceiveDataSize, this, [=](const qint64 nSize) {
            emit signalReceiveDataSize(ip, nSize);
        });
        connect(fileReceiver, &HiFileReceiver::signalReceiveError, this, [=](HiFileTransferDef::eRecevieError e) {
            emit signalReceiveError(ip, e);
        });
        connect(fileReceiver, &HiFileReceiver::signalReceiveStart, this, [=]() {
            emit signalReceiveStart(ip);
        });
        connect(fileReceiver, &HiFileReceiver::signalClose, this, [=]() {getFileReceiverManager()->destroyReceiver(fileReceiver); });
    }
}
